/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet.Test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author boomar
 */
public class AgeServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AgeServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AgeServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");
        String age = request.getParameter("age");
        if (nom != null && prenom != null) {
            out.println("<html><body>");
            out.println("<h1>Exemple de r&eacute;sultat</h1>");
            out.println("Bonjour " + nom + " " + prenom);
            out.println("</body></html>");
        } else {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AgeServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<p color=\"red\">" + ((nom == null && prenom == null) ? "" : (nom == null ? prenom : nom)) + "</p>");
            out.println("<form method=\"POST\">");
            out.println("Nom : <input type=\"text\" name=\"nom\" value=\"" + (nom == null ? "" : nom) + "\">");
            out.println("Prenom : <input type=\"text\" name=\"prenom\" value=\"" + (prenom == null ? "" : prenom) + "\">");
            out.println("Age : <input type=\"text\" name=\"prenom\" value=\"" + (age == null ? "" : age) + "\">");
            out.println("<input type=\"submit\" value=\"valider\">");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        }
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");
        String age = request.getParameter("age");

        if ( !nom.isEmpty() && !prenom.isEmpty()) {
            out.println("<html><body>");
            out.println("<h1>Exemple de r&eacute;sultat</h1>");
            out.println("Bonjour " + nom + " " + prenom);
            out.println("</body></html>");
        } else {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AgeServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form method=\"POST\">");
            out.println("Nom : <input type=\"text\" name=\"nom\" value=\"" + (nom.isEmpty() ? "" : nom) + "\">");
            out.println("Prenom : <input type=\"text\" name=\"prenom\" value=\"" + (prenom.isEmpty() ? "" : prenom) + "\">");
            out.println("Prenom : <input type=\"text\" name=\"prenom\" value=\"" + (age.isEmpty() ? "" : age) + "\">");
            out.println("<p color=\"red\"> Veuillez renseigner le(s) champs " + ((nom.isEmpty() && prenom.isEmpty()) ? "" : (nom.isEmpty() ? "nom" : "prenom")) + "</p>");
            out.println("<input type=\"submit\" value=\"valider\">");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        }
        out.close();
    }
}
